<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>授权结果</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet">
  <style>
    html,
    body {
      padding: 0;
      margin: 0;
      width: 100%;
      height: 100%;
    }
    .model-body-placeholder{
      color: #c7c7c7;
      user-select: none;
    }
    .hide{
      display: none;
    }
  </style>
</head>

<body>
  <div class="modal" id="model" tabindex="-1" data-backdrop="static"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">授权码(access_token)</h5>
        </div>
        <div class="modal-body">
          <p class="model-body-placeholder">授权码将会展示在这</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary">点击复制</button>
          <button type="button" class="btn btn-success hide">复制成功</button>
          <button type="button" class="btn btn-danger hide">复制失败</button>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/popper.js/2.6.0/umd/popper.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.6/clipboard.js"></script>
  <script>
    $(document).ready(function () {
      var param = window.location.href.split('?')[1]
      if (param) {
        var codes = param.split('&')
        for (const item of codes) {
          if (item.includes('code=')) {
            getAccessToken(item.replace('code=', ''))
          }
        }
      }

      function getAccessToken(code) {
        $.ajax({
          url: 'https://gitee.com/oauth/token',
          method: 'post',
          data: {
            grant_type: 'authorization_code',
            code,
            client_id: '9e241bd64197ec3da6ec4e36bfa3d2171a9ebc27d35790c8795c5fd43e2b92c7',
            redirect_uri: 'https://madong001.gitee.io/chromepluginhelper/access.html',
            client_secret: '3db2f0d9bc4df7560abbbedda758cebedb01ef9b568914a9879358b6632b7c38'
          },
          success: (res) => {
            if (res.access_token) {
              $('.modal-body').text(res.access_token)
              $('#model').modal({
                show: true
              })
              initCopyBtn()
            }
          },
          error: (err) => {
            $('.modal-body').text('授权码获取失败，请重试')
            $('#model').modal({
              show: true
            })
          }
        })
      }

      function initCopyBtn() {
        let clipboard = new ClipboardJS('.btn-primary', {
          // 通过target指定要复印的节点
          target: function () {
            return document.querySelector('.modal-body')
          }
        });

        clipboard.on('success', function (e) {
          $('.btn').hide()
          $('.btn-success').show()
          setTimeout(() => {
            $('.btn').hide()
            $('.btn-primary').show()
          }, 3000);
        });

        clipboard.on('error', function (e) {
          $('.btn').hide()
          $('.btn-danger').show()
          setTimeout(() => {
            $('.btn').hide()
            $('.btn-primary').show()
          }, 3000);
        });
      }
    })
  </script>
</body>

</html>